package com.lin.bank.service.impl;

import com.alibaba.cloud.commons.lang.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lin.bank.config.exception.BusinessException;
import com.lin.bank.config.reponse.ResultCode;
import com.lin.bank.entity.TransactionRecord;
import com.lin.bank.feign.RecordsFeign;
import com.lin.bank.mapper.TransactionRecordMapper;
import com.lin.bank.service.TransactionRecordService;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.nio.charset.CoderResult;
import java.text.SimpleDateFormat;
import java.util.Date;

@Component
@Service
@Slf4j
public class TransactionRecordServiceImpl extends ServiceImpl<TransactionRecordMapper, TransactionRecord> implements TransactionRecordService {

    @Resource
    private RabbitTemplate rabbitTemplate;

    @Override
    public IPage<TransactionRecord> selectPage(String cardNo, String transactionDate1, String transactionDate2, Integer current, Integer size) {
        QueryWrapper<TransactionRecord> wrapper = new QueryWrapper<>();
        Page<TransactionRecord> recordPage = new Page<>(current, size);
        IPage<TransactionRecord> recordIPage = null;
        if (StringUtils.isNotEmpty(cardNo) && StringUtils.isNotEmpty(transactionDate1) && StringUtils.isNotEmpty(transactionDate2)) {
            wrapper.eq("cardNo", cardNo);
            wrapper.between("transactionDate", transactionDate1, transactionDate2);
            recordIPage = page(recordPage, wrapper);
        } else {
            throw new BusinessException(ResultCode.PARAM_IS_BLANK.getCode(),
                    ResultCode.PARAM_IS_BLANK.getMessage());
        }
        return recordIPage;
    }

    @Override
    public String sendMsg(String cardNo, String cardNo2, Double amount,Double balance1,Double balance2) {
        String msg = cardNo + "," + cardNo2 + "," + amount+ "," + balance1+ "," + balance2;
        //转发并且发送消息
        rabbitTemplate.convertAndSend("xExchange", "", msg);
        log.info("发送了消息：" + msg);
        return "发送消息成功";
    }



    @Override
    public void receive(Message message, Channel channel) {
        String msg = new String(message.getBody());
        log.info("当前时间：{},收到队列信息{}", new Date().toString(), msg);
        String[] split = msg.split(",");
        String cardNo1 = split[0];
        String cardNo2 = split[1];
        String amount = split[2];
        String balance1 = split[3];
        String balance2 = split[4];
        String type=new String("取款");
        log.info("消息接收完毕！");
        //添加取款记录
        this.addRecords(cardNo1, Double.parseDouble(amount), type, new String("转账给" + cardNo2),Double.parseDouble(balance1));
        //添加存款记录
        this.addRecords(cardNo2, Double.parseDouble(amount), "存款", ("收到"+cardNo1+"的转账"),Double.parseDouble(balance2));


    }

    public void addRecords(String cardNo, Double amount, String transactionType, String remark,Double balance1) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String format = sdf.format(new Date());
        Date data = java.sql.Date.valueOf(format);
        TransactionRecord entity = new TransactionRecord(cardNo, data, amount, balance1, transactionType, remark);
        int insert = this.baseMapper.insert(entity);
    }

}
